from collections import namedtuple

### TOKENS ###
t_TOKEN001 = r'[0-9]+'
t_TOKEN002 = r'\('
t_TOKEN003 = r'\)'
t_TOKEN004 = r'\*'
t_TOKEN005 = r'\+'
t_TOKEN006 = r'-'
t_TOKEN007 = r'true'
t_TOKEN008 = r'false'
t_TOKEN009 = r'='
t_TOKEN010 = r'<='
t_TOKEN011 = r'!'
t_TOKEN012 = r'&'

### PRECEDENCE ###
precedence = ()

### RULES ###
def p_RULE001(p): # aexp : /[0-9]+/ > (Numeral n:$1)
    "aexp : TOKEN001"
    p[0] = Numeral(n=p[1])

def p_RULE002(p): # aexp : '(' aexp ')' > $2
    "aexp : TOKEN002 aexp TOKEN003"
    p[0] = p[2]

def p_RULE003(p): # aexp : aexp '*' aexp > (Multiplication a1=$1 a2=$3)
    "aexp : aexp TOKEN004 aexp"
    p[0] = Multiplication(a1=p[1], a2=p[3])
    
def p_RULE004(p): # aexp : aexp '+' aexp > (Addition a1=$1 a2=$3)
    "aexp : aexp TOKEN005 aexp"
    p[0] = Addition(a1=p[1], a2=p[3])

def p_RULE005(p): # aexp : aexp '-' aexp > (Subtraction a1=$1 a2=$3)
    "aexp : aexp TOKEN006 aexp"
    p[0] = Subtraction(a1=p[1], a2=p[3])
	
### AST ###
class ArithmeticExpression:
    pass

class Numeral(namedtuple('Numeral', 'n'), ArithmeticExpression):
    pass

class Multiplication(namedtuple('Multiplication', 'a1 a2'), ArithmeticExpression):
    pass

class Addition(namedtuple('Addition', 'a1 a2'), ArithmeticExpression):
    pass

class Subtraction(namedtuple('Subtraction', 'a1 a2'), ArithmeticExpression):
    pass
